/**
 * Collaborative Story Development Web Application (CSDApp) 
 * Copyright Anton Strack 2014
 *
 * This file is part of Collaborative Story Development Application (CSDApp).
 *
 * CSDApp is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or any later version.
 *
 * CSDApp is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * CSDApp. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package csdwa;

import java.sql.Connection;
import java.util.List;

/**
 * This class handles adding categories to posts.
 *
 * @author Anton Strack
 */
public class CategoryManager {

    private Connection conn;
    protected String requestResultMessage;
    private CategoryPostMapperDao cpmDao;
    private CategoryDao cDao;
    private int postResourceType;

    public CategoryManager(Connection conn, int postResourceType) {
        this.conn = conn;
        this.cpmDao = new CategoryPostMapperDao(conn, postResourceType);
        this.cDao = new CategoryDao(conn);
        this.postResourceType = postResourceType;
    }

    /**
     * Attaches a categories list to the request object for the specific bean
     * stored in the request bean object.
     *
     * @param request the request object containing a bean
     * @return the request object with post categories added if the bean had
     * any.
     */
    public CsdwaRequest attachPostCategories(CsdwaRequest request) {

        Resource bean = (Resource) request.getBean();
        if (bean != null) {
            this.cpmDao.selectCategoriesByPostId(bean.getId(), true);
            request.setAttribute("postCategories", this.cpmDao.getCategoryDao().getBeans());
        }
        return request;
    }

    /**
     * Get the post categories from the category post mapper dao.
     *
     * @return
     */
    public List<Category> getPostCategories() {
        return this.cpmDao.getCategoryDao().getBeans();
    }

    /**
     * Get the posts from the category post mapper dao that belong to the
     * category.
     *
     * @return
     */
    public List<? extends Post> getCategoryPosts() {
        return this.cpmDao.getPostDao().getBeans();
    }
}
